package com.primaryedu.ui.app.parentsch;

import com.bumptech.glide.Glide;
import com.primaryedu.R;
import com.primaryedu.bean.VoteoptionBean;
import com.primaryedu.data.Contants;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 投票详情
 *
 * @author Thomas
 * @description
 * @data 2018年1月8日
 */
public class VoteDetailsAdapter extends BaseAdapter implements View.OnClickListener {

    private final String totalVoteNum;
    private LayoutInflater inflater;
    @SuppressWarnings("unused")
    private Context context;
    private Callback mCallback;
    private List<VoteoptionBean.DataBean.ListBean> list = new ArrayList<>();
    private List<VoteoptionBean.DataBean> datalist = new ArrayList<>();
    private String result;

    //响应按钮点击事件,调用自定义接口，并传入View
    @Override
    public void onClick(View v) {
        mCallback.click(v);
    }

    /**
     * 自定义接口，用于回调按钮点击事件到Activity
     *
     * @author yy
     *         2018-02-02
     */
    public interface Callback {
        public void click(View v);
    }

    public VoteDetailsAdapter(Activity context, List<VoteoptionBean.DataBean.ListBean> list, String totalVoteNum, Callback callback) {
        this.context = context;
        this.list = list;
        this.totalVoteNum = totalVoteNum;
        this.inflater = LayoutInflater.from(context);
        mCallback = callback;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.item_vote_details, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_number_opinion = (TextView) convertView.findViewById(R.id.tv_number_opinion);
            viewHolder.image_voteopinion = (ImageView) convertView.findViewById(R.id.image_voteopinion);
            viewHolder.tv_name_opinion = (TextView) convertView.findViewById(R.id.tv_name_opinion);
            viewHolder.progressBar = (ProgressBar) convertView.findViewById(R.id.progressBarHorizontal);
            viewHolder.tv_persent = (TextView) convertView.findViewById(R.id.tv_persent);
            viewHolder.bt_vote = (Button) convertView.findViewById(R.id.bt_vote);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        VoteoptionBean.DataBean.ListBean ob = list.get(position);
        //序号
        viewHolder.tv_number_opinion.setText(String.valueOf(ob.getSort()));
        //图片
        Glide.with(convertView.getContext())
                .load(Contants.course_api + ob.getPic())
                .placeholder(R.drawable.vote_person_photo)
                .error(R.drawable.vote_person_photo)
                .centerCrop()
                .crossFade()
                .into(viewHolder.image_voteopinion);
        //姓名
        viewHolder.tv_name_opinion.setText(ob.getOptionName());

        //创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(0);
        //百分比
        if (totalVoteNum.equals("0")) {
            result = "0";
        } else {
            result = numberFormat.format((float) ob.getVoteNum() / (float) (Integer.parseInt(totalVoteNum)) * 100);
        }
        //进度条
        viewHolder.progressBar.setProgress(Integer.parseInt(result));
        //百分比
        viewHolder.tv_persent.setText(result + "%");
        Log.i("yy", "persentgetView: =========" + result);
        Log.i("yy", "getView: totalVoteNum=========" + totalVoteNum);
        Log.i("yy", "getView: getVoteNum=========" + ob.getVoteNum());
//		if(position == 1){
//			viewHolder.progressBar.setProgress(30);
//		}else if (position == 2) {
//			viewHolder.progressBar.setProgress(40);
//		}else if (position == 3) {
//			viewHolder.progressBar.setProgress(20);
//		}

        viewHolder.bt_vote.setOnClickListener(this);
        viewHolder.bt_vote.setTag(position);
        return convertView;
    }

    class ViewHolder {
        TextView tv_number_opinion;
        ImageView image_voteopinion;
        ProgressBar progressBar;
        Button bt_vote;
        TextView tv_name_opinion;
        TextView tv_persent;
    }
}
